package sale;

import book.BookManager;
import bookshopSM.Logon;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.Serializable;
import java.sql.*;
import java.util.Vector;

public class SaleBook extends JPanel{
    static  Connection conn = null;
    private static String username ="root";
    private static String password="root" ;
    private static String driverClassName="com.mysql.jdbc.Driver";
    private static String url="jdbc:mysql://127.0.0.1:3307/books_system";
    float vdiscount;
    int bnosum[][];
    JLabel lvno,ltitle,lbno,lbnum,lmoney,ltitlein,ldiscount,lrank;
    JTextField tfvno,tfbno,tfbnum,tfmoney,tfdiscount,tfrank;
    JButton bbno,writeButton,bclear,writeButton1;
    JPanel p,pin;
    int i=0;
    float money=0;
    String swno;
    private Vector rowData, columnName;
    private JTable jt = null;
    private JScrollPane jsp = null;
    protected Object[][] data;
    // protected String[] colname = {"图书编号","书名","数量(本)","定价(元)","活动价(元)","售价(元)"};
    public SaleBook(){
        ltitlein=new JLabel();
        ltitlein.setFont(new java.awt.Font("楷体",1,15));
        swno=Logon.getStf();
        p=new JPanel();
        pin = new JPanel();
        pin.setBounds(0, 0, 200, 200);
        pin.setVisible(true);
        p.setLayout(new BorderLayout());
        pin.setLayout(new BorderLayout());
        columnName = new Vector();
        bnosum=new int[100][2];
        //设置列名
        columnName.add("图书编号");
        columnName.add("书名");
        columnName.add("数量(本)");
        columnName.add("定价(元)");
        columnName.add("活动价(元)");
        lrank=new JLabel("      会员等级:");
        ldiscount=new JLabel("       折扣额度");
        ltitle=new JLabel("销售图书");
        ltitle.setFont(new java.awt.Font("楷体",1,20));
        lvno=new JLabel("            请输入会员号：      ");
        lbno=new JLabel("请输入图书编号：");
        JLabel lblank = new JLabel("             ");
        lbnum=new JLabel("    数量(本)：");
        lmoney=new JLabel("                            总计(元)：");
        tfmoney=new JTextField(6);
        tfmoney.setEditable(false);
        tfvno=new JTextField(10);
        tfbno=new JTextField(10);
        tfbnum=new JTextField(5);
        tfdiscount=new JTextField(5);
        tfrank=new JTextField(5);
        bbno=new JButton("添加");
        writeButton=new JButton( "确认交易" );
        writeButton1=new JButton( "确认" );
        bclear=new JButton("取消");
        tfrank.setEditable(false);
        tfdiscount.setEditable(false);
        Box box1=Box.createHorizontalBox();
        Box box2=Box.createHorizontalBox();
        Box box5=Box.createHorizontalBox();
        box1.add(lvno);
        box1.add(tfvno);
        box1.add(lblank);
        box1.add(writeButton1);
        box5.add(lrank);
        box5.add(tfrank);
        box5.add(ldiscount);
        box5.add(tfdiscount);
        box2.add(lbno);
        box2.add(tfbno);
        box2.add(lbnum);
        box2.add(tfbnum);
        box2.add(bbno);
        pin.add(ltitlein,BorderLayout.NORTH);
        p.add(box2,BorderLayout.NORTH);
        p.add(pin,BorderLayout.CENTER);
        Box box4=Box.createHorizontalBox();
        box4.add(writeButton);
        box4.add(bclear);
        box4.add(lmoney);
        box4.add(tfmoney);
        p.add(box4,BorderLayout.SOUTH);
        //p.add()
        Box box3=Box.createVerticalBox();
        box3.add(Box.createVerticalStrut(15));
        box3.add(ltitle);
        box3.add(Box.createVerticalStrut(15));
        box3.add(box1);
        box3.add(Box.createVerticalStrut(10));
        box3.add(box5);
        box3.add(Box.createVerticalStrut(10));
        box3.add(p);
        this.add(box3);

        bbno.addActionListener(
                new ActionListener() {

                    public void actionPerformed( ActionEvent event )
                    {
                        addOneRecord();
                    }}
        );
        writeButton.addActionListener(
                new ActionListener() {
                    public void actionPerformed( ActionEvent event )
                    {
                        addAllRecord();
                        DisplayRecord();
                    }
                }


        );
        writeButton1.addActionListener(
                new ActionListener() {
                    public void actionPerformed( ActionEvent event )
                    {
                        selectRecord();
                    }
                }


        );
        bclear.addActionListener(
                new ActionListener() {
                    public void actionPerformed( ActionEvent event )
                    {
                        clearRecord();
                    }
                }


        );
    }
    protected void selectRecord() {
        try {
            ResultSet rs;
            conn = DriverManager.getConnection(url,username,password);
            Statement stmt=conn.createStatement();
            rs = stmt.executeQuery("select * " +
                    " from VIP " +
                    " where vno ="+Integer.valueOf(tfvno.getText()));
            rs.next();
            vdiscount=rs.getFloat("Vdiscount");
            tfrank.setText(rs.getString("Vrank"));
            tfdiscount.setText(String.valueOf(vdiscount));
        }
        catch(SQLException e1){
            JOptionPane.showMessageDialog(null, "无相应记录");
            System.out.println(e1);
        }
        catch (Exception ee){
            System.out.println(ee);
        }
        // TODO Auto-generated method stub

    }
    protected void clearRecord() {
        // TODO Auto-generated method stub
        i=0;money=0;
        tfvno.setText("");
        tfbno.setText("");
        tfbnum.setText("");
        pin.removeAll();
        tfmoney.setText("");
        p.repaint();
    }
    //"图书编号","书名","数量(本)","定价(元)","活动价(元)","售价(元)"
    protected void DisplayRecord() {
        // TODO Auto-generated method stub
        rowData = new Vector<Serializable>();
        try {
            ResultSet rs;
            conn = DriverManager.getConnection(url,username,password);
            Statement stmt=conn.createStatement();

            for (int j=0;j<i;j++){
                rs = stmt.executeQuery("select * " +
                        " from Book " +
                        " where Bno ="+bnosum[j][0]);
                rs.next();//rs指向第一行之前
                Vector<String> line1 = new Vector<String>();
                line1.add(String.valueOf(rs.getInt("Bno")));
                line1.add(rs.getString("Bname"));
                line1.add(String.valueOf(bnosum[j][1]));
                line1.add(String.valueOf(rs.getFloat("Bprice")));
                line1.add(String.valueOf(rs.getFloat("Bsellprice")));
                rowData.add(line1);
                money=money+vdiscount*rs.getFloat("Bsellprice")*bnosum[j][1];
            }
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "请输入会员号:");
            //e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            //e.printStackTrace();
            JOptionPane.showMessageDialog(null, "没有该会员");
        }

        //  System.out.print("aa");
        tfmoney.setText(String.valueOf(money));
        jt = new JTable(rowData, columnName);
        //jsp.removeAll();
        jsp = new JScrollPane(jt);
        ltitlein.setText("账单：");
        pin.add(jsp,BorderLayout.CENTER);
        setSize( 300, 100 );
        p.validate();
    }
    protected void addAllRecord() {
        try {
            conn = DriverManager.getConnection(url,username,password);
            Statement stmt=conn.createStatement();
            for(int j=0;j<i;j++){
                stmt.executeUpdate("INSERT INTO sale(Vno,Bno,Wno,Snum) " +
                        " VALUES ("+Integer.valueOf(tfvno.getText())+","+
                        bnosum[j][0]+","+
                        Integer.valueOf(swno)+","+
                        bnosum[j][1]+
                        ")");
            }
            JOptionPane.showMessageDialog(null, "交易成功");
        }
        catch(SQLException e1){
            JOptionPane.showMessageDialog(null, "请输入正确的图书编号");
            System.out.println(e1);
        }
        catch (Exception ee){
            System.out.println(ee);
        }
        finally{
            if(conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                conn=null;
            }
        }
        // TODO Auto-generated method stub

    }
    protected void addOneRecord() {
        // TODO Auto-generated method stub
        int num = 0;
        //System.out.print(vnoDelete);
        try {
            ResultSet rs;
            conn = DriverManager.getConnection(url,username,password);
            Statement stmt=conn.createStatement();
            rs = stmt.executeQuery("select * " +
                    " from Book " +
                    " where Bno ="+Integer.valueOf(tfbno.getText()));
            rs.next();//rs指向第一行之前
            num=rs.getInt("Bnum");
            System.out.print(String.valueOf(num));

            if(num-Integer.valueOf(tfbnum.getText())>=0){
                BookManager bookmanager=new  BookManager();
                //图书编号","书名","数量(本)","定价(元)","活动价(元)","售价(元)
                bnosum[i][0]=Integer.valueOf(tfbno.getText());
                bnosum[i++][1]=Integer.valueOf(tfbnum.getText());
                tfbno.setText("");
                tfbnum.setText("");
                //JOptionPane.showMessageDialog(null, "添加成功");
            }
            else{
                JOptionPane.showMessageDialog(null, "库存不足");
            }
        } catch (NumberFormatException e) {
            // TODO Auto-generated catch block
            //e.printStackTrace();

        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "请输入正确的图书编号");
            // TODO Auto-generated catch block
            //00e.printStackTrace();
        }

    }

}
